package org.seven.jrdp.biz.sys.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.seven.jrdp.biz.sys.mapper.SysRoleMapper;
import org.seven.jrdp.biz.sys.mapper.SysRoleResMapper;
import org.seven.jrdp.biz.sys.mapper.SysRoleRoleMapper;
import org.seven.jrdp.biz.sys.mapper.SysUserRoleMapper;
import org.seven.jrdp.biz.sys.model.SysRole;
import org.seven.jrdp.biz.sys.model.SysRoleRes;
import org.seven.jrdp.biz.sys.model.SysRoleRole;
import org.seven.jrdp.biz.sys.model.SysUserRole;
import org.seven.jrdp.commons.exception.BizException;
import org.seven.jrdp.commons.model.TreeNode;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@CacheConfig(cacheNames = "role")
@Service
public class SysRoleService {
	@Autowired
	private SysRoleMapper sysRoleMapper;
	@Autowired
	private SysRoleResMapper sysRoleResMapper;
	@Autowired
	private SysRoleRoleMapper sysRoleRoleMapper;
	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;

	@Transactional(readOnly = true)
	public List<SysRole> find() {
		Example example = new Example(SysRole.class);
		example.orderBy("code");
		return sysRoleMapper.selectByExample(example);
	}

	@Transactional(readOnly = true)
	public SysRole get(String id) {
		return sysRoleMapper.selectByPrimaryKey(id);
	}

	@CacheEvict(cacheNames = { "role", "user", "mgt" }, allEntries = true)
	@Transactional
	public void insert(SysRole sysRole) {
		sysRoleMapper.insertSelective(sysRole);
	}

	@CacheEvict(cacheNames = { "role", "user", "mgt" }, allEntries = true)
	@Transactional
	public void update(SysRole sysRole) {
		sysRoleMapper.updateByPrimaryKeySelective(sysRole);
	}

	@CacheEvict(cacheNames = { "role", "user", "mgt" }, allEntries = true)
	@Transactional
	public void delete(String id) {
		Example example = new Example(SysRole.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("pid", id);
		if (sysRoleMapper.selectCountByExample(example) > 0) {
			throw new BizException("该角色下存在子角色，不允许删除");
		}
		sysRoleMapper.deleteByPrimaryKey(id);
		SysRoleRes sysRoleRes = new SysRoleRes();
		sysRoleRes.setRoleId(id);
		sysRoleResMapper.delete(sysRoleRes);
		SysRoleRole sysRoleRole = new SysRoleRole();
		sysRoleRole.setRoleId(id);
		sysRoleRoleMapper.delete(sysRoleRole);
		SysUserRole sysUserRole = new SysUserRole();
		sysUserRole.setRoleId(id);
		sysUserRoleMapper.delete(sysUserRole);
	}

	@CacheEvict(cacheNames = { "role", "user", "mgt" }, allEntries = true)
	@Transactional
	public void saveRes(String id, String[] resIds) {
		{
			SysRoleRes sysRoleRes = new SysRoleRes();
			sysRoleRes.setRoleId(id);
			sysRoleResMapper.delete(sysRoleRes);
		}
		if (resIds != null) {
			for (String resId : resIds) {
				SysRoleRes sysRoleRes = new SysRoleRes();
				sysRoleRes.setRoleId(id);
				sysRoleRes.setResId(resId);
				sysRoleResMapper.insertSelective(sysRoleRes);
			}
		}
	}

	@CacheEvict(cacheNames = { "role", "user", "mgt" }, allEntries = true)
	@Transactional
	public void saveRole(String id, String[] roleIds) {
		{
			SysRoleRole sysRoleRole = new SysRoleRole();
			sysRoleRole.setRoleId(id);
			sysRoleRoleMapper.delete(sysRoleRole);
		}
		if (roleIds != null) {
			for (String roleId : roleIds) {
				SysRoleRole sysRoleRole = new SysRoleRole();
				sysRoleRole.setRoleId(id);
				sysRoleRole.setRelRoleId(roleId);
				sysRoleRoleMapper.insertSelective(sysRoleRole);
			}
		}
	}

	@Cacheable(key = "'role_tree'")
	@Transactional(readOnly = true)
	public List<TreeNode> findTree() {
		return sysRoleMapper.findTree();
	}

	@Cacheable(key = "'role_tree_'+#userId")
	@Transactional(readOnly = true)
	public List<TreeNode> findTreeByUserId(String userId) {
		return sysRoleMapper.findTreeByUserId(userId);
	}

	@Cacheable(key = "'role_res_tree_'+#id")
	@Transactional(readOnly = true)
	public List<TreeNode> findResTreeById(String id) {
		return sysRoleMapper.findResTreeById(id);
	}

	@Cacheable(key = "'role_role_tree_'+#id")
	@Transactional(readOnly = true)
	public List<TreeNode> findRoleTreeById(String id) {
		return sysRoleMapper.findRoleTreeById(id);
	}
}